<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="treeData" :props="{ label: 'name' }" default-expand-all :expand-on-click-node="false">
        <template #default="{ data }">
          <el-row :style="{ width: '100%' }" type="flex" justify="space-between">
            <div>{{ data.name }}</div>
            <div class="right">
              <el-row style="width: 140px;" type="flex" justify="space-between">
                <span>{{ data.managerName }}</span>
                <el-dropdown @command="onChange($event, data.id)">
                  <span class=" el-dropdown-link">
                    操作
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                    <el-dropdown-item command="del">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>

              </el-row>
            </div>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDiloag ref="addDep" :visible.sync="visible" :current-id="currentId" @refresh="loadData" />
  </div>
</template>
<script>
import { delDepartmentAPI, getDepartmentAPI } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDiloag from './AddDiloag.vue'

export default {
  name: 'Department',
  components: {
    AddDiloag
  },
  data() {
    return {
      visible: false,
      treeData: [],
      currentId: ''
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    async loadData() {
      const res = await getDepartmentAPI()
      console.log(res)
      const xxx = transListToTreeData(res, 0)
      this.treeData = xxx
    },
    async onChange(command, id) {
      if (command === 'add') {
        this.visible = true
        this.currentId = id
      }
      if (command === 'edit') {
        await this.$refs.addDep.getDepDetail(id)
        this.visible = true
      }
      if (command === 'del') {
        await this.$confirm('删除吗', '温馨提示')
        await delDepartmentAPI(id)
        this.$message.success('删除成功')
        this.loadData()
      }
    }
  }
}
</script>

<style>

.container {
  padding: 30px 140px;
  margin: 20px;
  background-color: white;
}
</style>

